var Myaddress = decodeURI(window.location.href).split('=');
var MyaddressDetail = Myaddress[4];
var provinceId;
var cityId;
var countyId;
var addressUrl = decodeURI(decodeURI(window.location.href)).split('&');
var ifEdit = GetQueryString('addressId');
var ifAdd = getItem('ifAdd');
//是否获取到地区
$(function(){
    if(getItem('save_add_name')==""||getItem('save_add_name')==undefined||getItem('save_add_name')==null){
    }else{
        $('#name').val(getItem('save_add_name'))
        $('#phone').val(getItem('save_add_phone'))
    }
    if(ifEdit==""||ifEdit==undefined||ifEdit==null){
        $('#save').show();
        $('#save2').hide();
        if(getItem('save_add_city')==undefined||getItem('save_add_city')==null||getItem('save_add_city')==""){
            $('.fl').show();
            $('#address').hide();
            provinceId = '';
            cityId = '';
            countyId = '';
        }else{
            if(ifAdd=='true'){}else{
                $('#address').val(getItem('save_add_city'));
                $('#detail').val(getItem('save_add_detail'));
                $('.fl').hide();
                $('#address').show();
            }
        }
    }else{
        $('#save').hide();
        $('#save2').show();
        $('.mui-title').html('编辑地址');
        var name = addressUrl[1].split('=')[1];
        var phone = addressUrl[2].split('=')[1];
        var detail = addressUrl[3].split('=')[1];
        var detailBox = addressUrl[4].split('=')[1];
        $('#name').val(name);
        $('#phone').val(phone);
        $('#address').val(getItem('save_add_city'));
        $('#detail').val(getItem('save_add_detail'));
        $('.fl').hide();
        $('#address').show();
        provinceId = GetQueryString('proId');
        cityId = GetQueryString('cityId');
        countyId = GetQueryString('countyId');
    }
})

var isIos = 0;

$(function () {
    //ios
    setupWebViewJavascriptBridge(function(bridge) {
        /* Initialize your app here */
        bridge.callHandler('getDevice', function(responseData) {
            isIos = 1;
        })
    })

})

//所在地区
$('.fl,.location').click(function () {
    if(ifApp()==0||ifApp()==2){
        //微信
        address();
        window.location.href="nearby.html"
    }else if(ifApp()==1){
        /*ios*/
        address();
        if(isIos==1){
            setupWebViewJavascriptBridge(function(bridge) {
                /* Initialize your app here */
                bridge.callHandler('getLocation', function(responseData) {
                    setLocation(responseData);
                })
                return;
            })
        }else{
            window.location.href="nearby.html"
        }
        return;
    }else{
        //其他
        address()
        window.location.href="nearby.html"
    }
})

function address() {
    if($('#name').val()==""||$('#name').val()==null||$('#name').val()==undefined){
    }else{
        setItem('save_add_name',$('#name').val());
    }
    if($('#phone').val()==""||$('#phone').val()==null||$('#phone').val()==undefined){
    }else{
        setItem('save_add_phone',$('#phone').val());
    }
}

//ios存储地址cookie
function setLocation(data) {
    setItem('location',encodeURI(data.location));
    setItem('save_add_city',data.city);
    setItem('save_add_detail',data.address);
    setItem('ifAdd','false');
    window.location.reload();
}

//添加保存
$('#save').click(function () {
    var ifDefault = '';
    if($('#ifDefault').hasClass('mui-active')){
        ifDefault = 1;
    }else{
        ifDefault = 0;
    }
    $.ajax({
        url:'/user/addUserAddress.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            city:getItem('save_add_city'),
            location:getItem('location'),
            ifDefault:ifDefault,
            name:$('#name').val(),
            phone:$('#phone').val(),
            detail:$('#detail').val(),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('保存地址成功',function () {
                    removeItem('save_add_name');
                    removeItem('save_add_phone');
                   //window.location.href=getItem('setUrl');
                    history.go(-2);
                });
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})

//编辑保存
$('#save2').click(function () {
    var ifDefault = '';
    if($('#ifDefault').hasClass('mui-active')){
        ifDefault = 1;
    }else{
        ifDefault = 0;
    }
    $.ajax({
        url:'/user/editUserAddress.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            addressId:ifEdit,
            city:getItem('save_add_city'),
            location:getItem('location'),
            ifDefault:ifDefault,
            name:$('#name').val(),
            phone:$('#phone').val(),
            detail:$('#detail').val(),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('保存地址成功',function () {
                    removeItem('save_add_name');
                    removeItem('save_add_phone');
                    history.go(-2);
                });
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})